The main entry point for fittr. Takes a set of base model specifications and a list of definitions describing DVs, regression functions, and optional extras, then runs all combinations in parallel.
Usage
fit_models(
models,
data,
defs,
weight_var = NULL,
weighted_only = FALSE,
workers = NULL,
plan = future::multisession,
furrr_opts = furrr::furrr_options()
)Arguments
- models
A named list of
model_specobjects, one per model to run. Names become the keys in the returned results list.- data
A data frame, or a named list of data frames. When a list is provided, every model is run once per dataset.
- defs
A named list of definition lists. Each element must contain:
dvCharacter. The dependent variable name.
funFunction (or named list of functions) with signature
f(formula, data, weights), or a character string naming a function (e.g."lm"or"fixest::feols"). A string is wrapped intofunction(f, d, w, ...) <fun>(f, data = d, weights = w, ...), The formula is passed as the first argument, whatever that argument is called. Arguments set withset_argsare passed as further named arguments, so a function fitting such a model needs...or arguments of those names.
and may optionally contain:
extra_termsCharacter vector of terms appended to every model's main terms (but not to its parts; see
set_parts) for this DV only.additional_modelsNamed list of
model_specobjects added to the model set for this DV only.model_modsNamed list of functions of the form
function(model_spec) -> model_spec. Each entry modifies the named base model for this DV only, leaving other DVs unchanged.
- weight_var
Character. Name of a column in
datato use as regression weights. When supplied, each model is run twice: once unweighted and once weighted. DefaultNULL(no weights).- weighted_only
Logical. When
TRUEandweight_varis supplied, only the weighted variants are run. DefaultFALSE.- workers
Positive integer or
NULL(default). When provided, fittr callsfuture::plan(plan, workers = workers)internally, dispatches all runs (all DVs \(\times\) models \(\times\) datasets \(\times\) weights) in a single parallelfurrr::future_pmapcall, then restores the previous plan on exit. WhenNULL, DVs are processed sequentially and within-DV parallelism uses whateverfuture::plan()the caller has already set.- plan
A future strategy passed to
future::plan()whenworkersis notNULL. Defaults tofuture::multisession(new background R sessions, works on all platforms). Other options includefuture::multicore(forked processes, Unix/macOS only, lower overhead) orfuture::cluster(pre-existing worker cluster). Ignored whenworkers = NULL.- furrr_opts
A
furrr_optionsobject controlling furrr behaviour (seed, packages, globals, scheduling, etc.). Passed as the.optionsargument tofuture_pmap. Defaultfurrr::furrr_options().
Value
A list (one element per definition) of named lists. Each inner list
has names of the form "model.dataset.weights" (components are
omitted when there is only one dataset or no weighting).
Examples
base_m <- model_spec(cyl, disp)
models <- list(
base = base_m,
ext = base_m |> add_terms(hp),
base_int = base_m |> add_interaction(cyl, disp)
)
data_sets <- list(all = mtcars, high_mpg = mtcars[mtcars$mpg > 20, ])
defs <- list(
mpg = list(
dv = "mpg",
fun = "lm",
# Remove disp from the extended model for mpg only
model_mods = list(ext = \(m) drop_terms(m, disp))
),
carb = list(
dv = "carb",
fun = function(f, d, w) stats::lm(f, d, weights = w)
)
)
results <- fit_models(models, data_sets, defs)